//
//  IMPlainSDKFileHelper.swift
//  ActorSDK
//
//  Created by 孙继刚 on 16/2/27.
//  Copyright © 2016年 Madordie. All rights reserved.
//

import Foundation

public class IMPlainFileHander: NSObject {

    static var sharedHanderList =  [IMPlainFileHander]()
    static func addHander(hander: IMPlainFileHander) {
        sharedHanderList.append(hander)
    }
    static func removeHander(hander: IMPlainFileHander?) {
        guard hander == nil else { return }
        if let index = sharedHanderList.indexOf(hander!) {
            sharedHanderList.removeAtIndex(index)
        }
    }
    
    public func autoStart(start: Bool = true) -> IMPlainFileHander {
        self.AAFileCallBackObj = AAFileCallback(onDownloaded: { [weak self] (reference) -> () in
            let data = NSFileManager.defaultManager().contentsAtPath(CocoaFiles.pathFromDescriptor(reference))
            self?.onceCallback?(fileHander: self!, data: data)
            IMPlainFileHander.removeHander(self)
            })
        IMPlainFileHander.addHander(self)
        Actor.bindRawFileWithReference(self.fileReference, autoStart: start, withCallback: self.AAFileCallBackObj)
        return self
    }
    
    var AAFileCallBackObj: AAFileCallback?
    let fileReference: ACFileReference?
    let onceCallback: ((fileHander: IMPlainFileHander, data: NSData?) -> Void)?
    
    public init(reference: ACFileReference!, onceCallback: ((fileHander: IMPlainFileHander, data: NSData?) -> Void)?) {
        self.fileReference = reference
        self.onceCallback  = onceCallback
    }
    
    //  MARK: - Media
    
    public class func fileRemoteSource(ACFileRemoteSource file: ACFileRemoteSource!,
        notDownloaded:(fileRemote: ACFileRemoteSource) -> Void,
        onDownloading: (fileRemote: ACFileRemoteSource, progress: Double) -> Void,
        onDownloaded: (fileRemote: ACFileRemoteSource) -> Void) {
            
            Actor.requestStateWithFileId(file.getFileReference().getFileId(), withCallback: AAFileCallback(
                notDownloaded: { () -> () in
                    Actor.startDownloadingWithReference(file.getFileReference())
                    notDownloaded(fileRemote: file)
                }, onDownloading: { (progress) -> () in
                    Actor.cancelDownloadingWithFileId(file.getFileReference().getFileId())
                    onDownloading(fileRemote: file, progress: progress)
                }, onDownloaded: { (reference) -> () in
                    onDownloaded(fileRemote: file)
            }))
    }
    
    public class func fileLocalSource(ACMessage_rid rid: jlong,
        notDownloaded:(ACMessage_rid: jlong) -> Void,
        onDownloading: (ACMessage_rid: jlong, progress: Double) -> Void,
        onDownloaded: (ACMessage_rid: jlong) -> Void) {
            Actor.requestUploadStateWithRid(rid, withCallback: AAUploadFileCallback(
                notUploaded: { () -> () in
                    Actor.resumeUploadWithRid(rid)
                    notDownloaded(ACMessage_rid: rid)
                }, onUploading: { (progress) -> () in
                    Actor.pauseUploadWithRid(rid)
                    onDownloading(ACMessage_rid: rid, progress: progress)
                }, onUploadedClosure: { () -> () in
                    onDownloaded(ACMessage_rid: rid)
                }))
    }
}